<?php

namespace App\Http\Models\Api;

use App\Http\Common\Base\BaseModel;
use App\Http\Common\Config\PlatformConfig;

class ApiUserModel extends BaseModel
{
    protected $table = 'user';

    /**
     * 验证是否存在用户
     * @param $verityValue
     * @param int $type
     * @param array $field
     * @return null
     */
    public static function verifyUser($verityValue, $type = PlatformConfig::PLATFORM_ANDROID, $field = ['*'])
    {
        $verityKey = 'username';
        if ($type == PlatformConfig::PLATFORM_WE_CHAT) $verityKey = 'wx_open_id';

        if (empty($field)) $field = ['*'];

        $data = self::where($verityKey, $verityValue)->first($field);

        if (empty($data)) return null;
        return $data;
    }


    /**
     * 构建用户缓存的数据
     * @param $user
     * @return array
     */
    public static function createCacheData($user)
    {
        $userMessage = array(
            'userId'    => $user->id,
            'username'  => $user->username,
            'telephone' => $user->telephone,
            'nickname'  => $user->nickname,
            'avatar'    => $user->avatar,
            'sex'       => $user->sex,
            'wxOpenId'  => $user->wx_open_id,
            'qqOpenId'  => $user->qq_open_id,
        );
        return $userMessage;
    }

}
